import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop_z/config/service_url.dart';
import 'package:flutter_shop_z/model/commodity_cart.dart';
import 'package:flutter_shop_z/pages/cart_page/bloc/cart_bloc.dart';
import 'package:flutter_shop_z/pages/cart_page/cart_count.dart';
import 'package:flutter_shop_z/shared_preferences/commodity_cart_prefs.dart';
import 'package:shared_preferences/shared_preferences.dart';

// class CartItem extends StatefulWidget {
//   CommodityCart cart;
//   List<CommodityCart> cartList;
//   int index;
//   CartItem(this.cart, this.cartList, this.index);
//   _CartItemState createState() =>
//       _CartItemState(this.cart, this.cartList, this.index);
// }

class CartItem extends StatelessWidget {
  CommodityCart cart;
  List<CommodityCart> cartList;
  int index;
  CartItem(this.cart, this.cartList, this.index);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(5, 2, 5, 2),
      padding: EdgeInsets.fromLTRB(5, 10, 5, 10),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(bottom: BorderSide(width: 1, color: Colors.black12)),
      ),
      child: Row(
        children: [
          _cartChexcBt(context),
          _cartImage(),
          _cartItemName(),
          _cartItemPrice(context),
        ],
      ),
    );
  }

//多选按钮
  Widget _cartChexcBt(context) {
    return Checkbox(
      value: cart.isCheck,
      onChanged: (bool val) {
        cart.isCheck = val;
        CommodityCartPrefs.checkCartState(cart);
        _updateCommodityBloc(context);
      },
      activeColor: Colors.purple,
    );
  }

  //图片
  Widget _cartImage() {
    return Container(
      width: ScreenUtil().setWidth(150),
      padding: EdgeInsets.all(3.0),
      decoration:
          BoxDecoration(border: Border.all(width: 1, color: Colors.black12)),
      child: Image.network(
          "${serviceUrl}goods/${cart.categoryId}/${cart.categorySubId}/${cart.commodityImg}"),
    );
  }

  //商品名称
  Widget _cartItemName() {
    return Container(
      width: ScreenUtil().setWidth(300),
      padding: EdgeInsets.all(10),
      alignment: Alignment.topLeft,
      child: Column(
        children: [
          Text(
            cart.commodityName,
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(fontSize: ScreenUtil().setSp(28)),
          ),
          CartCount(cart),
        ],
      ),
    );
  }

//商品价格
  Widget _cartItemPrice(context) {
    return Container(
      width: ScreenUtil().setWidth(150),
      alignment: Alignment.centerRight,
      child: Column(
        children: <Widget>[
          Text("￥${cart.commodityDiscountPrice}"),
          Container(
            child: InkWell(
              onTap: () {
                //删除商品
                CommodityCartPrefs.deleteCommodity(cart.commodityId);
                //获得删除后的列表
                //更新bloc
                _updateCommodityBloc(context);
              },
              child: Icon(
                Icons.delete_forever,
                color: Colors.black26,
                size: 30,
              ),
            ),
          ),
        ],
      ),
    );
  }

//更新bloc
  void _updateCommodityBloc(context) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String listStr = prefs.getString(CommodityCartPrefs.cartListKey);
    BlocProvider.of<CartBloc>(context).add(SetCartEvent(listStr));
  }
}

// class _CartItemState extends State<CartItem> {
//   CommodityCart cart;
//   List<CommodityCart> cartList;
//   int index;

//   _CartItemState(this.cart, this.cartList, this.index);
//   @override
//   Widget build(BuildContext context) {
//     print("item==cart.isCheck===>${cart.isCheck}");
//     if (cartList == null || cartList.length < 0) {
//       print("cartList==null");
//     } else {
//       print("cartList.length==${cartList.length}");
//     }
//     // print("item==cartList[index].isCheck===>${cartList[index].isCheck}");
//     return Container(
//       margin: EdgeInsets.fromLTRB(5, 2, 5, 2),
//       padding: EdgeInsets.fromLTRB(5, 10, 5, 10),
//       decoration: BoxDecoration(
//         color: Colors.white,
//         border: Border(bottom: BorderSide(width: 1, color: Colors.black12)),
//       ),
//       child: Row(
//         children: [
//           _cartChexcBt(),
//           _cartImage(),
//           _cartItemName(),
//           _cartItemPrice(),
//         ],
//       ),
//     );
//   }

// //多选按钮
//   Widget _cartChexcBt() {
//     return Checkbox(
//       value: cart.isCheck,
//       onChanged: (bool val) {
//         cart.isCheck = val;
//         CommodityCartPrefs.checkCartState(cart);
//         _updateCommodityBloc();
//       },
//       activeColor: Colors.purple,
//     );
//   }

//   //图片
//   Widget _cartImage() {
//     return Container(
//       width: ScreenUtil().setWidth(150),
//       padding: EdgeInsets.all(3.0),
//       decoration:
//           BoxDecoration(border: Border.all(width: 1, color: Colors.black12)),
//       child: Image.network(
//           "${serviceUrl}goods/${cart.categoryId}/${cart.categorySubId}/${cart.commodityImg}"),
//     );
//   }

//   //商品名称
//   Widget _cartItemName() {
//     return Container(
//       width: ScreenUtil().setWidth(300),
//       padding: EdgeInsets.all(10),
//       alignment: Alignment.topLeft,
//       child: Column(
//         children: [
//           Text(
//             cart.commodityName,
//             maxLines: 2,
//             overflow: TextOverflow.ellipsis,
//             style: TextStyle(fontSize: ScreenUtil().setSp(28)),
//           ),
//           CartCount(cart),
//         ],
//       ),
//     );
//   }

// //商品价格
//   Widget _cartItemPrice() {
//     return Container(
//       width: ScreenUtil().setWidth(150),
//       alignment: Alignment.centerRight,
//       child: Column(
//         children: <Widget>[
//           Text("￥${cart.commodityDiscountPrice}"),
//           Container(
//             child: InkWell(
//               onTap: () {
//                 //删除商品
//                 CommodityCartPrefs.deleteCommodity(cart.commodityId);
//                 //获得删除后的列表
//                 //更新bloc
//                 _updateCommodityBloc();
//               },
//               child: Icon(
//                 Icons.delete_forever,
//                 color: Colors.black26,
//                 size: 30,
//               ),
//             ),
//           ),
//         ],
//       ),
//     );
//   }

// //更新bloc
//   void _updateCommodityBloc() async {
//     SharedPreferences prefs = await SharedPreferences.getInstance();
//     String listStr = prefs.getString(CommodityCartPrefs.cartListKey);
//     BlocProvider.of<CartBloc>(context).add(SetCartEvent(listStr));
//   }
// }
